﻿using Pay.Core.exception;
using Pay.Model.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Model.Dto
{
    /// 全部参数为纯小写,遇到单词用下划线隔开

    /// <summary>
    /// 发起支付请求参数
    /// </summary>
    public class PayInput
    {
        /// <summary>
        /// 商户号
        /// </summary>
        public string mch_code { get; set; }
        /// <summary>
        /// 商户的订单号
        /// </summary>
        public string mch_trade_no { get; set; }
        /// <summary>
        /// 支付方式
        /// </summary>
        public PayType pay_type { get; set; }
        /// <summary>
        /// 自定义参数
        /// </summary>
        public string param { get; set; }
        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal amount { get; set; }
        /// <summary>
        /// 异步回调地址
        /// </summary>
        public string notify_url { get; set; }
        /// <summary>
        /// 同步回调地址
        /// </summary>
        public string return_url { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public int timespan { get; set; }
        /// <summary>
        /// 签名
        /// </summary>
        public string sign { get; set; }

        public bool Check()
        {
            if (string.IsNullOrWhiteSpace(mch_trade_no))
                throw new LotException("订单编号不能为空");
            if (mch_trade_no.Length > 32)
                throw new LotException("订单编号长度不能大于32位");
            if (amount <= 0)
                throw new LotException("订单金额不能是负数");
            if (amount > 10000)
                throw new LotException("订单金额不能超过一万");
            if (!string.IsNullOrWhiteSpace(param) && param.Length > 128)
                throw new LotException("自定义参数长度不能超过128");
            if (string.IsNullOrWhiteSpace(notify_url))
                throw new LotException("异步通知地址不能为空");
            if (notify_url.Length > 128)
                throw new LotException("异步通知地址长度不能超过128");
            if (string.IsNullOrWhiteSpace(return_url))
                throw new LotException("同步通知地址不能为空");
            if (return_url.Length > 128)
                throw new LotException("同步通知地址长度不能超过128");
            if (timespan <= 0)
                throw new LotException($"时间戳错误,不能是{timespan}");
            if (string.IsNullOrWhiteSpace(sign))
                throw new LotException("签名不能为空");

            return true;
        }
    }

    /// <summary>
    /// 发起支付返回参数
    /// </summary>
    public class PayOutput
    {
        /// <summary>
        /// 请求状态
        /// </summary>
        public AlipayOrderStatus status { get; set; }
        /// <summary>
        /// 返回消息
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 平台交易单号
        /// </summary>
        public string trade_no { get; set; }
        /// <summary>
        /// 商家订单号
        /// </summary>
        public string mch_trade_no { get; set; }
        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal amount { get; set; }
        /// <summary>
        /// 实际需要支付的金额
        /// </summary>
        public decimal pay_amount { get; set; }
        /// <summary>
        /// 支付方式
        /// </summary>
        public PayType pay_type { get; set; }
        /// <summary>
        /// 服务器时间
        /// </summary>
        public int server_time { get; set; }
        /// <summary>
        /// 订单过期时间
        /// </summary>
        public int expire_time { get; set; }
        /// <summary>
        /// 二维码地址
        /// </summary>
        public string qrcode { get; set; }
    }


}
